package com.atguigu.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;

/**
 * @ClassName: KeyWordUtil
 * @Description:
 * @Author: kele
 * @Date: 2021/4/27 13:18
 *
 *
 *  使用ik分词器，将搜索的text转换为word
 **/
public class KeyWordUtil {

    public static HashSet<String> analyzeWord(String words) {

        StringReader reader = new StringReader(words);

        /**
         * 参数1：传入一个数据输入的reader
         * 参数2：是否使用智能的模式
         */
        IKSegmenter ikSegmenter = new IKSegmenter(reader,true);

        HashSet<String> wordsSet = new HashSet<>();

        try {

            Lexeme lexeme = ikSegmenter.next();
            while (lexeme != null){

                String word = lexeme.getLexemeText();
                wordsSet.add(word);

                lexeme = ikSegmenter.next();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return wordsSet;
    }

//测试例
    public static void main(String[] args) {
        System.out.println(KeyWordUtil.analyzeWord("小米手机手机"));
    }

}

